package com.lbd.smsplatform.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.smsplatform.bean.SmsTemplate;
import com.lbd.smsplatform.service.SmsTemplateService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * Created by zzl on 2016/9/29.
 */
@RestController
@RequestMapping("/smsTemplate")
public class SmsTemplateController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    SmsTemplateService smsTemplateService;

    /**
     * 保存短信模板信息
     *
     * @param smsTemplate
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(SmsTemplate smsTemplate) {
        smsTemplateService.save(smsTemplate);
        return new Result(ResultState.SUCCESS, "成功！");

    }

    /**
     * 分页查询短信模板信息
     *
     * @param page        第几页
     * @param rows        一页多少条
     * @param smsTemplate 查询条件
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, SmsTemplate smsTemplate) {
        return new Result(ResultState.SUCCESS, "查询成功",
                smsTemplateService.findByPageConditionJpql(page, rows, smsTemplate));

    }

    /**
     * 删除短信模板信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        System.out.println(Arrays.toString(ids));
        smsTemplateService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");
    }

    /**
     * 根据ID查询要编辑的短信模板信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", smsTemplateService.findById(id));
    }

    /**
     * 修改短信模板信息
     *
     * @param smsTemplate
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(SmsTemplate smsTemplate) {
        smsTemplateService.update(smsTemplate);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

}
